//
//  XVVIndexSelectUse.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/16.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVIndexSelectUse.h"

@interface XVVIndexSelectUse ()<UITableViewDelegate,UITableViewDataSource,DKSortArrayDataSource>

@property (nonatomic,strong) UIImageView *tableviewBackview;

@property (nonatomic,strong) UITableView *tableview;

@end

@implementation XVVIndexSelectUse
{
    NSMutableArray *sortConditionArray;
    DKSortArray *dkSort;
}

#pragma mark getter
-(UIImageView*)tableviewBackview
{
    if (_tableviewBackview==nil)
    {
        _tableviewBackview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"商贸配对背景图"]];
        _tableviewBackview.contentMode = UIViewContentModeScaleToFill;
    }
    return _tableviewBackview;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundView = _tableviewBackview;
        _tableview.sectionIndexBackgroundColor = [UIColor clearColor];
        _tableview.sectionIndexColor = [XXTool colorWithProject];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [self addSubview:_tableview];
    }
    return _tableview;
}

-(DKButton*)backButton
{
    if (_backButton==nil)
    {
        _backButton = [[DKButton alloc] init];
        _backButton.imagePosition = DKButtonImagePositionAtEnd;
        _backButton.image = [UIImage imageNamed:@"箭头"];
        _backButton.imageTintColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:0.2];
        _backButton.imageSize = CGSizeMake(13, 80/3);
        _backButton.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.2];
        _backButton.layer.cornerRadius = 5;
        _backButton.font = [XXTool systemFontOfSize:1];
        [_backButton setTintColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]];
        [_backButton addTarget:self Select:@selector(clickBackButton) ForEvent:DKButtonEventPressedEnd];
        [self addSubview:_backButton];
    }
    return _backButton;
}

#pragma mark setter
-(void)setIndexDataList:(NSArray *)indexDataList
{
    _indexDataList = indexDataList;
    [self sortExhibitorList];
}

-(void)setNowType:(XVVIndexSelectUseType)nowType
{
    _nowType = nowType;
    if (_nowType==XVVIndexSelectShowSearchResult)
    {
        _tableview.backgroundView = nil;
    }
    else
    {
        _tableview.backgroundView = _tableviewBackview;
    }
    [_tableview reloadData];
}

#pragma mark sub
-(void)initParameter
{
    dkSort = [[DKSortArray alloc] init];
    dkSort.dataSource = self;
}

-(void)initUI
{
    [self tableviewBackview];
    [self tableview];
    [self backButton];
}

-(void)refreshLayout:(CGRect)frame
{
    _tableview.frame = CGRectMake(0, 0, frame.size.width
                                  , frame.size.height);
    _backButton.frame = CGRectMake(-10
                                    , (frame.size.height-80)/2
                                    , 30
                                    , 80);
}

/**
 *  data排序
 */
-(void)sortExhibitorList
{
    sortConditionArray = [[NSMutableArray alloc] init];
    
    if (_nowType==XVVIndexSelectShowSearchResult)
    {
        for (RXMLExhibitorInformationModel *temp in _indexDataList)
        {
            if ([XXTool didCurrentLanguageChinese])
            {
                [sortConditionArray addObject:temp.CompanyName_chn];
            }
            else
            {
                [sortConditionArray addObject:temp.CompanyName_eng];
            }
        }
    }
    else
    {
        for (RXMLExhibitorIndexModel *temp in _indexDataList)
        {
            if ([XXTool didCurrentLanguageChinese])
            {
                [sortConditionArray addObject:temp.index_name_cn];
            }
            else
            {
                [sortConditionArray addObject:temp.index_name_en];
            }
        }
    }
    
    [dkSort setArray:sortConditionArray];
    [dkSort sort];
   
    [_tableview reloadData];
    
}

#pragma mark DkSortDatasource
-(NSString*)DKSortArray:(DKSortArray *)object stringOfSortArray:(NSArray *)dataArray AtIndex:(NSUInteger)index
{
    return sortConditionArray[index];
}

#pragma mark tableviewDatacource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCShowIndex *cell = [XTCShowIndex cellWithTableView:tableView];
    [cell setCellWithData:_indexDataList[[dkSort indexOfSection:indexPath.section AtIndex:indexPath.row]]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XTCShowIndex getCellHeightWithData:_indexDataList[[dkSort indexOfSection:indexPath.section AtIndex:indexPath.row]]];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [dkSort numberOfSection];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dkSort numberOfRowAtSection:section];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35/2;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return  nil;
    }
    
    UILabel * label = [[UILabel alloc] init];
    label.frame = CGRectMake(15, 0,self.frame.size.width-15, 35/2);
    label.font=[XXTool systemFontOfSize:14];
    label.text = sectionTitle;
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0
                                                                    , 0
                                                                    , self.frame.size.width
                                                                    , 35/2)];
    if (_nowType==XVVIndexSelectShowSearchResult)
    {
        [sectionView setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]];
    }
    else
    {
        [sectionView setBackgroundColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.3]];
    }
    [sectionView addSubview:label];
    return sectionView;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [dkSort getTitleArray][section];
}

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [dkSort getTitleArray];
}

#pragma mark tableveiwDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if ([_delegate respondsToSelector:@selector(indexSelectUse:dataList:clickTableviewIndexPathRow:)])
    {
        [_delegate indexSelectUse:self
                         dataList:_indexDataList
       clickTableviewIndexPathRow:[dkSort indexOfSection:indexPath.section AtIndex:indexPath.row]];
    }
}

-(void)clickBackButton
{
    [UIView animateWithDuration:0.4f
                     animations:^{
                         self.frame = CGRectMake(self.frame.size.width
                                                 , 0
                                                 , self.frame.size.width
                                                 , self.frame.size.height);
                     } completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}

#pragma mark api
+(void)newIndexSelectUseWithSourceVC:(UIViewController *)sourceVC indexDataList:(NSArray *)indexDataList useType:(XVVIndexSelectUseType)type completion:(void (^)())block
{
    XVVIndexSelectUse *view = [[XVVIndexSelectUse alloc] init];
    view.frame = CGRectMake(sourceVC.view.frame.size.width
                            , 0, sourceVC.view.frame.size.width
                            , sourceVC.view.frame.size.height);
    [view setNowType:type];
    [view setIndexDataList:indexDataList];
    view.delegate = sourceVC;
    if (view)
    {
        [sourceVC.view addSubview:view];
        [UIView animateWithDuration:0.4f
                         animations:^{
                             view.frame = CGRectMake(0, 0, view.frame.size.width
                                                     , view.frame.size.height);
                         } completion:^(BOOL finished) {
                             if (block)
                             {
                                 block();
                             }
                         }];
    }
}

@end
